<template>
	<view class="box bg_f2">
		<view class="bg_f2 top_box">
			<view class="top_wrapper bg_f2">
				<view class="top_block" @click="get_block(item)" :class="{'active': item.id == top_tab_active}"
					v-for="(item, index) in top_tab_list" :key="index">
					<view class="top_block_text">
						<view class="font_31">{{item.name}}</view>
						<view></view>
					</view>
					<view class="line bg_d2" v-if="index !== 2"></view>
				</view>
			</view>
			<view class="flex9 top_times" @click="is_show = true;">
				<view class="flex7 order_p1">
					<view class="font_27 color_7b" style="margin-right: 10rpx;">{{mouth}}</view>
					<van-icon name="arrow-down" color="#7B7B7B" />
				</view>
			</view>
		</view>
		<view v-if="is_load">
			<view v-if="data_list.length === 0">
				<image src="../../static/image/user/j@2x.png" class="no_data"></image>
			</view>
			<view class="content hidden" v-else>
				<view hover-class="none" class="list bg_fff" v-for="item in data_list" :key="item.id">
					<navigator class="list_top hidden" :url="'./detail?id=' + item.id">
						<view class="font_35 text_bold order_m2">{{item.status_text}} <span
								class="folat_laft color_ff6a00 ">查看详情</span></view>
						<view class="font_29 color_7b list_content">
							<view>寄存时间：{{item.start_time}}</view>
							<view>寄存物品：行李箱*{{item.trunk_number}} 手提包*{{item.packsack_number}}</view>
							<view>订单编号：{{item.order_no}}</view>
						</view>
					</navigator>
					<view class="van-hairline--top flex3 order_p2">
						<view class="flex3" style="flex: 3;" v-if="item.status == 1"
							@click="is_time_show = true;seletect_order_info=item">
							<!-- <view class="flex3 list_block">
								<view class="flex3" style="margin-right: 8rpx;">
									<van-icon name="clock-o" size="16" color="#7B7B7B"
										custom-style="font-weight: bold;" />
								</view>
								<view class="font_27 color_7b">{{item.time_}}</view>
							</view> -->
							<view class="flex3 list_block color_ff6a00" style="font-size: 12px;">点击填写寄存码</view>
						</view>
						<view class="flex3" v-else-if="item.status == 2"
							@click="is_time_show = true;seletect_order_info=item">
							<!-- <view class="flex3 list_block">
								<view class="flex3" style="margin-right: 8rpx;">
									<van-icon name="gold-coin-o" size="16" color="#7B7B7B"
										custom-style="font-weight: bold;" />
								</view>
								<view class="font_27 color_7b">{{item.merchants_amount}}元</view>
							</view> -->
							<view class="flex3 list_block color_ff6a00" style="font-size: 12px;">点击填写取件码</view>
						</view>
						<view class="w_flex1" v-else></view>
						<view class="flex3">
							<view class="flex3 list_block" @click.stop="get_phone(item.mobile)">
								<view class="flex3" style="margin-right: 8rpx;">
									<van-icon name="phone-circle-o" size="16" color="#7B7B7B"
										custom-style="font-weight: bold;" />
								</view>
								<view class="font_27 color_ff6a00 ">拨打电话</view>
							</view>
						</view>
					</view>
				</view>
				<view class="flex7 font_27 color_999 color_ff6a00" style="height: 100rpx;">{{Text}}</view>
			</view>
		</view>
		<!-- 年月 -->
		<van-popup position="bottom" :show="is_show">
			<van-datetime-picker type="year-month" :value="currentDate" :max-date="minDate" @cancel="is_show = false;"
				@confirm="get_time" />
		</van-popup>
		<van-popup :show="is_time_show">
			<view class="modal_wrapper hidden">
				<view class="modal_top">
					<view class="font_35 text_bold index_m7">
						{{seletect_order_info.status == 1?'请输入用户订单详情里寄存码': '请输入用户订单详情里取件码'}}
					</view>
					<view class="font_31 flex3 van-hairline--bottom">
						<view class="w_flex1 time_block">
							<input :placeholder="seletect_order_info.status == 1?'请输入用户订单中的寄存码': '请输入用户订单中的取件码'"
								v-model="code" />
						</view>
					</view>
				</view>
				<view class="van-hairline--top font_31 modal_foot">
					<view class="van-hairline--right" @click="is_time_show = false;">取消</view>
					<view @click="get_time_sure">确定</view>
				</view>
			</view>
		</van-popup>
	</view>
	<!-- 寄存码 -->
</template>

<script>
	export default {
		data() {
			return {
				mouth: '',
				page: 1,
				code: "",
				Text: '',
				data_list: [],
				is_load: false,
				seletect_order_info: {},
				is_show: false,
				top_tab_active: 1,
				is_time_show: false,
				is_last_page: false,
				minDate: new Date().getTime(),
				currentDate: new Date().getTime(),
				top_tab_list: [{
					id: 1,
					name: '待存放'
				}, {
					id: 2,
					name: '待取件'
				}, {
					id: 0,
					name: '全部'
				}, ]
			}
		},
		onLoad() {
			this.mouth = this.get_date(new Date().getTime());
			this.getOrdersList();
		},
		onShow() {
			if (uni.getStorageSync('merchantList')) {
				this.Text = '';
				this.page = 1;
				this.data_list = [];

				getApp().get_loading();
				this.getOrdersList();

				uni.removeStorageSync('merchantList');
			}
		},
		onReachBottom() { //上拉加载
			if (!this.is_last_page) {
				this.page++;
				this.Text = '加载中...';
				this.getOrdersList();
			}
		},
		methods: {
			// 商家订单
			getOrdersList() {
				this.$http.post(this.$api.getOrdersList, {
					page: this.page,
					mouth: this.mouth,
					status: this.top_tab_active
				}).then(res => {
					this.is_load = true;

					if (res.data.data.length > 0) {
						res.data.data.forEach((n, k) => {
							n.time_ = this.get_day_time(n.createtime_text);
						})
					}

					this.data_list = this.data_list.concat(res.data.data);

					if (this.page == res.data.last_page) {
						this.is_last_page = true;
						this.Text = '已加载全部'
					} else {
						this.is_last_page = false;
					}
				})
			},
			get_block(e) {
				this.Text = '';
				this.page = 1;
				this.data_list = [];
				this.top_tab_active = e.id;

				getApp().get_loading();
				this.getOrdersList();
			},
			// 年月
			get_time(e) {
				this.Text = '';
				this.page = 1;
				this.data_list = [];
				this.is_show = false;
				this.mouth = this.get_date(e.detail);

				getApp().get_loading();
				this.getOrdersList();
			},
			get_date(value) {
				var date = new Date(value);
				var Y = date.getFullYear();
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
				var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
				var h = date.getHours();
				var m = date.getMinutes();
				var s = date.getSeconds();

				return Y + '-' + M;
			},
			// 打电话
			get_phone(e) {
				uni.makePhoneCall({
					phoneNumber: e
				});
			},
			// 转天 时间 分钟
			get_day_time(e) {
				let self = this;
				let time_num = '';

				var s1 = new Date(e.replace(/-/g, "/")),
					s2 = new Date(),
					runTime = parseInt((s2.getTime() - s1.getTime()) / 1000);
				var year = Math.floor(runTime / 86400 / 365);
				runTime = runTime % (86400 * 365);
				var month = Math.floor(runTime / 86400 / 30);
				runTime = runTime % (86400 * 30);
				var day = Math.floor(runTime / 86400);
				runTime = runTime % 86400;
				var hour = Math.floor(runTime / 3600);
				runTime = runTime % 3600;
				var minute = Math.floor(runTime / 60);
				runTime = runTime % 60;
				var second = runTime;

				if (year) {
					time_num = year + '年' + month + '月' + day + '天' + hour + '小时' + minute + '分钟';
				} else if (month) {
					time_num = month + '月' + day + '天' + hour + '小时' + minute + '分钟';
				} else if (day) {
					time_num = day + '天' + hour + '小时' + minute + '分钟';
				} else if (hour) {
					time_num = hour + '小时' + minute + '分钟';
				} else if (minute) {
					time_num = minute + '分钟';
				}

				return time_num;
			},
			// 寄存/取件
			get_time_sure() {
				if (!this.code) return getApp().get_msg('请输入内容');
				this.is_time_show = false;
				getApp().get_loading();
				if (this.seletect_order_info.status == 1) {
					// 待存件
					this.$http.post(this.$api.ordersConsign, {
						order_id: this.seletect_order_info.id,
						check_code: this.code
					}).then(res => {
						getApp().get_msg('存件成功');
						this.get_block();

						uni.setStorageSync('merchantList', true);
					})
				}

				if (this.seletect_order_info.status == 2) {
					// 待取件
					this.$http.post(this.$api.ordersCollect, {
						order_id: this.seletect_order_info.id,
						collect_code: this.code
					}).then(res => {
						getApp().get_msg('取件成功');
						this.get_block();
						uni.setStorageSync('merchantList', true);
					})
				}

			}
		}
	}
</script>

<style lang="less">
	@import url("/static/css/top.css");

	.folat_laft {
		float: right;
		// border: 1px solid;
		// border-radius: 10px;
		// padding: 0 10px;

		color: #FF6A00;
		width: 170rpx;
		box-shadow: none;
		font-size: 27rpx;
		background: #fff;
		border-radius: 30rpx;
		text-align: center;
		border: 1px solid #FF6A00;
	}

	.order_p1 {
		padding: 10rpx;
	}

	.order_p2 {
		padding: 9rpx 0;
	}

	.order_m2 {
		margin: 31rpx 0 33rpx;
	}

	.box {
		box-sizing: border-box;
		padding: 154rpx 0 0;
	}

	.top_box {
		top: 0;
		left: 0;
		right: 0;
		z-index: 99;
		height: 154rpx;
		position: fixed;
	}

	.top_times {
		top: 96rpx;
		right: 36rpx;
		z-index: 99;
		position: fixed;
	}

	.top_block {
		padding-bottom: 13rpx;
	}

	.no_data {
		width: 492rpx;
		height: 414rpx;
		margin: 240rpx auto 0;
	}

	.list {
		width: 704rpx;
		border-radius: 29rpx;
		margin: 20rpx auto;

		&:first-child {
			margin-top: 140rpx;
		}

		.list_top {
			padding: 0 23rpx;
		}

		.list_content {
			view {
				margin: 22rpx auto;
			}
		}

		.list_block {
			// margin: 0 0 0 10rpx;
			padding: 14rpx 23rpx;
		}
	}

	.box /deep/ .van-picker__cancel {
		color: #999;
	}

	.box /deep/ .van-picker__confirm {
		color: #FF6A00;
	}

	.box /deep/ .van-hairline--top:after {
		border-top-color: #D2D2D2;
	}

	.box .van-popup--center {
		width: 615rpx;
		border-radius: 23rpx;
	}

	.modal_wrapper {
		text-align: center;

		.time_block {
			padding: 17rpx 0;

			input {
				width: 100%;
			}
		}

		.modal_top {
			padding: 0 46rpx;
		}

		.modal_foot {
			display: flex;
			margin: 60rpx 0 0;

			view {
				flex: 1;
				height: 106rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				&:last-child {
					color: #576B95;
				}
			}
		}
	}

	.box {
		box-sizing: border-box;
		padding: 0 0 134rpx 0;
	}

	.index_m7 {
		margin: 62rpx 0 45rpx;
	}

	.detail_m1 {
		margin: 12rpx 0 0 0;
	}

	.content {
		padding: 0 23rpx;
	}

	.top {
		display: flex;
		margin: 20rpx 0;
		height: 221rpx;
		align-items: center;
		border-radius: 15rpx;
		flex-direction: column;
		justify-content: center;
	}

	.wrapper {
		margin: 20rpx 0;
		border-radius: 15rpx;
	}

	.user_wrapper {
		padding: 31rpx 23rpx;

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			margin-right: 23rpx;
		}

		.phone_wrapper {
			height: 100rpx;
			margin-left: 50rpx;

			view {
				padding: 10rpx 0 0;
			}
		}
	}

	.title {
		margin: 31rpx 0 4rpx 0;

		view {
			&:first-child {
				width: 4rpx;
				height: 33rpx;
				border-radius: 6rpx;
				margin-right: 19rpx;
				background-color: #EE8332;
			}
		}
	}

	.van-icon-phone-circle-o:before {
		content: "\F098";
		color: #FF6A00
	}

	.text_wrapper {
		overflow: hidden;
		padding: 0 23rpx;

		.text_list {
			margin: 23rpx 0;

			&:last-child {
				margin-bottom: 31rpx;
			}

			&:first-child {
				margin-top: 31rpx;
			}
		}
	}
</style>
